/**
 * FileName: answer_1
 * Author:   why
 * Date:     2019/3/13 11:39
 * Description:
 */
package algorithm.leetcode.problem_3;

import java.util.HashSet;
import java.util.Set;

public class answer_1 {


    class Solution {
        public int lengthOfLongestSubstring(String s) {
            Set<Character> occ = new HashSet<>();
            int n = s.length();
            int rk = -1, ans = 0;
            for (int i = 0; i < n; i++) {
                if (i != 0) {
                    occ.remove(s.charAt(i - 1));
                }
                while (rk + 1 < n && !occ.contains(s.charAt(rk + 1))) {
                    occ.add(s.charAt(rk + 1));
                    rk++;
                }
                ans = Math.max(ans, rk - i + 1);
            }
            return ans;
        }

    }
}
